package com.example.memo;

import android.app.Dialog;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;

import androidx.collection.ArraySet;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.memo.Bean.waitBean;
import com.example.memo.Services.ResultListener2;
import com.example.memo.db.MysqlOpenHelper;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class waitFragment extends Fragment implements View.OnClickListener {
    //定义数据对象
    private SQLiteDatabase sqLiteDatabase;

    private ImageView add_wait;
    //列表
    private ListView M_listViewID2;
    private ListView M_listViewID3;
    //定义数据对象
    private List<waitBean> wait_data=new ArrayList<>();
    private List<waitBean> wait_data2=new ArrayList<>();
    private Myadapter2 myadapter2;
    private Myadapter4 myadapter3;
    private Spinner spinner;

    //待办总数
    private TextView count;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_wait, container, false);
        //初始化
        initView(view);
        initData();
        //获取数据
        getData();
        //listView
        setListView();

        //下拉
        selection();


        return view;
    }

    void initView(View view){
        add_wait = view.findViewById(R.id.add2);
        add_wait.setOnClickListener(this::onClick);
        count = view.findViewById(R.id.count);
        //全部待办的下拉框
        spinner=view.findViewById(R.id.my_spinner_wait);
        //列表的初始化
        M_listViewID2=view.findViewById(R.id.listview);
        M_listViewID3=view.findViewById(R.id.listview2);

    }

    public void initData(){
        //插入数据时注意，一定要传入最新的数据库版本号
        MysqlOpenHelper  mysqlOpenHelper=new MysqlOpenHelper(getActivity());
        sqLiteDatabase=mysqlOpenHelper.getWritableDatabase();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.add2:
                myadapter2.showCustomDialog(-1);
                break;
        }
    }


   public  void getData() {
       String[] ziduan = {"neirong", "create_time", "type", "id"};
       Cursor cursor = sqLiteDatabase.query("wait_table", ziduan, null, null
               , null, null, null);
       if (cursor.getCount() > 0) {
           int value1 = cursor.getColumnIndex("neirong");
           int value2 = cursor.getColumnIndex("create_time");
           int value3 = cursor.getColumnIndex("type");
           int value4 = cursor.getColumnIndex("id");
           while (cursor.moveToNext()) {
               String nr = cursor.getString(value1);
               String time = cursor.getString(value2);
               int type = cursor.getInt(value3);
               String id = cursor.getString(value4);
               SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日 HH:mm");
               String format = dateFormat.format(new Date(time));
               waitBean waitBean = new waitBean();
               if(type==0){
                   waitBean.setId(id);
                   waitBean.setCreate_time(format);
                   waitBean.setType(String.valueOf(type));
                   waitBean.setNeirong(nr);
                   wait_data.add(waitBean);
               }else {
                   waitBean.setId(id);
                   waitBean.setCreate_time(format);
                   waitBean.setType(String.valueOf(type));
                   waitBean.setNeirong(nr);
                   wait_data2.add(waitBean);
               }
           }
       } else {
           Toast.makeText(getActivity(), "wait获取信息失败", Toast.LENGTH_SHORT).show();
       }
       cursor.close();
   }
   //下拉
    public void selection(){
        String[] items=getResources().getStringArray(R.array.waits);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), R.layout.activity_list_drop, items);
        //第一个参数表示在哪个Activity上显示，第二个参数是系统下拉框的样式，第三个参数是数组。
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }
    //设置listView
    public void  setListView(){
        myadapter2 = new Myadapter2(wait_data, getContext(), new ResultListener2() {
            @Override
            public void getWaitData(List<waitBean> lists) {
                    Log.e("TAG", "获取数据接口回调成功");
                    Log.e("TAG", lists.toString());
                    M_listViewID2.setAdapter(myadapter2);
                    wait_data = lists;
                    count.setText(String.valueOf(lists.size()) + "条待办");
            }

            @Override
            public void getWaitData2(List<waitBean> lists2) {

            }
        });

        myadapter3 = new Myadapter4(wait_data2, getContext(), new ResultListener2() {
            @Override
            public void getWaitData(List<waitBean> lists) {

            }
            @Override
            public void getWaitData2(List<waitBean> lists2) {
                    Log.e("TAG", "获取数据接口回调成功");
                    Log.e("TAG", lists2.toString());
                    M_listViewID3.setAdapter(myadapter3);
                    wait_data2=lists2;
            }
        });
        //加载适配器
        M_listViewID2.setAdapter(myadapter2);
        M_listViewID3.setAdapter(myadapter3);
        // 将ListView平滑地滚动到最底部
        M_listViewID2.smoothScrollToPosition(M_listViewID2.getAdapter().getCount() - 1);
        M_listViewID3.smoothScrollToPosition(M_listViewID3.getAdapter().getCount()-1);
        //设置待办总数
        count.setText(String.valueOf(wait_data.size())+"条待办");

    }

}